package com.zhanghe.study.mapreduce.friend;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.Arrays;

/**
 * @author zh
 * @date 2021/4/2 15:10
 */
public class TwoShareFriendsMapper extends Mapper<LongWritable,Text, Text,Text> {
    Text k = new Text();

    Text v = new Text();
    // 根据第一次的结果
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//        A	I,K,C,B,G,F,H,O,D
//        B	A,F,J,E
        // 每一行的数据  key都是  value的共同好友
        String[] fields = value.toString().split("\t");
        v.set(fields[0]);

        if(StringUtils.isNotBlank(fields[1])){
            String[] persons = fields[1].split(",");
            // 这里要排序  否则会出现 C&D  D&C分成了两项
            Arrays.sort(persons);
            for(int i = 0; i<persons.length;i++){
                for(int j = i+1;j<persons.length;j++){
                    k.set(persons[i]+"&"+persons[j]);
                    context.write(k,v);
                }
            }
        }
    }
}
